//
//  HEMissonListView.m
//  HealthFamily
//
//  Created by 吴欧 on 2020/10/15.
//  Copyright © 2020 honglutec. All rights reserved.
//

#import "HEMissonListView.h"
#import "HEMissonListModel.h"
#import "HEMoreView.h"
#import "HEMissionCell.h"
#import "HEHealthEduViewController.h"


@interface HEMissonListView ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, copy) NSMutableArray<HEMissonListModel *> *dataSourceArray;

@end

@implementation HEMissonListView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        
        self.backgroundColor = [UIColor colorWithRed:255/255.0 green:255/255.0 blue:255/255.0 alpha:1.0];
        self.layer.shadowColor = [UIColor colorWithRed:13/255.0 green:98/255.0 blue:218/255.0 alpha:0.08].CGColor;
        self.layer.shadowOffset = CGSizeMake(0,5);
        self.layer.shadowOpacity = 1;
        self.layer.shadowRadius = 6;
        self.layer.cornerRadius = 10;
                
        [self initUI];
    }
    return self;
}

- (NSMutableArray *)dataSourceArray {
    if (!_dataSourceArray) {
        _dataSourceArray = [NSMutableArray array];
    }
    return _dataSourceArray;
}

- (void)initUI {
    
    HEMoreView *allView = [[HEMoreView alloc] init];
    allView.title = @"健康资讯";
    [self addSubview:allView];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(initoInfoClick)];
    [allView addGestureRecognizer:tap];
    
    allView.sd_layout
    .topSpaceToView(self, 13)
    .leftSpaceToView(self, 15)
    .rightSpaceToView(self, 15)
    .heightIs(20);

    
    _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _tableView.scrollEnabled = NO;
    [self addSubview:_tableView];

    
    [_tableView registerClass:HEMissionCell.class forCellReuseIdentifier:@"HEMissionCell"];

    _tableView.sd_layout
    .topSpaceToView(allView, 9)
    .leftSpaceToView(self, 5)
    .rightSpaceToView(self, 5)
    .bottomSpaceToView(self, 5);
    
    [self loadData];
}

- (void)initoInfoClick {
    HEHealthEduViewController *vc = [HEHealthEduViewController new];
    vc.index = 0;
    [self.vc.navigationController pushViewController:vc animated:YES];
}

- (void)loadData {

    HEPageRequest *request = [[HEPageRequest alloc] init];
    request.pageNum = 1;
    request.pageSize = 4;
    request.queryParams = @{}.mutableCopy;
//    request.queryParams = @{@"video_type":@"健康宣教"}.mutableCopy;

    [WOAFNetWorkingHandle POSTWithBaseUrl:KServerUrl suffixUrl:KInfoList parameters:request success:^(id  _Nonnull responseObject) {
       
        [self.dataSourceArray removeAllObjects];
        HEPageResponse *response = [HEPageResponse mj_objectWithKeyValues:responseObject];
        [self.dataSourceArray addObjectsFromArray:[HEMissonListModel mj_objectArrayWithKeyValuesArray:response.data.list]];
        
        [self.tableView reloadData];
        if (self.heightBlock) {
            self.heightBlock(20 + 13 + 9 + 120 * self.dataSourceArray.count + 5);
        }
    } failure:^(NSError * _Nonnull error) {
    }];
}

- (void)getTableHeight:(returnTableHeight)block {
    self.heightBlock = block;
}

#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataSourceArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    HEMissionCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HEMissionCell"];
    cell.model = self.dataSourceArray[indexPath.row];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 120;
}

#pragma mark - UITableViewDelegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    HEMissonListModel *model = _dataSourceArray[indexPath.row];
    model.infoUrl = [NSString stringWithFormat:@"%@/details/#/text?id=%ld", KServerUrl, model.id];
    NSURL * file = [NSURL URLWithString: model.infoUrl];
    
    RootWebViewController *webView = [[RootWebViewController alloc] initWithUrl:file.absoluteString];
    webView.isShowCloseBtn = YES;
    webView.title = @"资讯详请";
    webView.showPageTitle = NO;
    webView.isShowShare = YES;
    webView.model = model;
    [self.vc.navigationController pushViewController:webView animated:YES];
}

@end
